package interfacePackageStructures;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;

import mapPackage.IVille;
import mapPackage.MapException;
import mapPackage.Ville;
import mapPackage.tailleVille.STailleVille;

import xmlPackage.Carte;

public class MyTableVilles extends AbstractTableModel {
	
	private String[] columnNames = {"Nom",
                                    "Taille", 
                                    "Touristique",
                                    "Ouverte"};
	
	ArrayList<String> data = new ArrayList<String>();
	
	public MyTableVilles(char lettre) {
		System.out.println("MyTableVilles lettre " + lettre);
		
		String test = lettre + "";
		data.clear();
		
		//synchronized(Carte.GetInstance()) {
			Iterator<String> villesIterator = Carte.GetInstance().getVillesIterator(test.toUpperCase());
			while (villesIterator.hasNext()) {
				String nomVille = villesIterator.next();
				data.add(nomVille);
			}
			villesIterator = Carte.GetInstance().getVillesIterator(test.toLowerCase());
			while (villesIterator.hasNext()) {
				String nomVille = villesIterator.next();
				data.add(nomVille);
			}
		//}
			fireTableDataChanged();
	}

	public void supprimerVille(int numRow) {
		String nomVille = data.get(numRow);
		data.remove(nomVille);
		try {
			Carte.GetInstance().supprimerVille(nomVille);
		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		fireTableRowsDeleted(numRow, numRow);
	}
	
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.size();
	}

    public String getColumnName(int col) {
        return columnNames[col];
    }

	public Object getValueAt(int row, int col) {
		try {
			String nomVille = data.get(row);
			IVille ville = Carte.GetInstance().getVille(nomVille);
			System.out.println("getValueAt row " + row + " col " + col);
	    	
			switch (col) {
			case 0:
				return ville.getNom();
			case 1:
				return ville.getTailleVille();
			case 2://tourisme
				return ville.isTouristique();
			case 3://ouverte
				return ville.isOuvert();
			default:
				return "";
			}
			
		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		/*if (col == 0) return data.get(row).getNom();
    	if (col == 1) return data.get(row).isAsuivre();
    	
    	else return "";*/
		return null;
	}
	
	/*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        System.out.println("isCellEditable");
    	return true;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {           

    	try {
			System.out.println("setValueAt row " + row + " col " + col);
	    	//si la ligne existe pas on la cree sinon modif
	    	if ((data.size() <= row) || (data.size() == 0 )){
	    			//TODOSOOH
	    			//data.add(new VilleIntermediaire((String)value));
	    		
	    	}
	    	else {
	    		String nomVille = data.get(row);
				IVille ville = Carte.GetInstance().getVille(nomVille);
				
				switch (col) {
				case 0:
					System.out.println("ville.setNom " + (String)value);
			    	ville.setNom((String)value);
					break;
				case 1:
					System.out.println("ville.setTailleVille " + value.toString());
			    	ville.setTailleVille((STailleVille)value);
					break;
				case 2://tourisme
					System.out.println("ville.setTourisme " +  (Boolean)value);
			    	ville.setTouristique((Boolean)value);
					break;
				case 3://ouverte
					System.out.println("ville.setOuverte " +  (Boolean)value);
			    	ville.setOuvert((Boolean)value);
					break;
				default:
				}
				ville.setModified(true);
	    		fireTableCellUpdated(row, col);
	    	}

		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public void ajouterVille(String nom, STailleVille taille, boolean isTouristique, boolean ouverte) {
		try {
			Carte.GetInstance().ajouterVille(new Ville(nom, taille, isTouristique));
			IVille ville = Carte.GetInstance().getVille(nom);
			ville.setOuvert(ouverte);
			ville.setModified(true);
	    	data.add(nom);
	    	fireTableRowsInserted(data.size() - 1, data.size() - 1);
		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
